package com.keyancompany.zhongbaopingtai.activity;

import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.TeamDetailModel;
import com.keyancompany.zhongbaopingtai.model.TeamRewardModel;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.MenuPopuWindow;
import com.keyancompany.zhongbaopingtai.weight.YaoQingMemberPopup;

import butterknife.Bind;
import butterknife.OnClick;

public class TeamIntroductionActivity extends BaseActivity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.team_name)
    TextView teamName;
    @Bind(R.id.team_create_time)
    TextView teamCreateTime;
    @Bind(R.id.team_creator)
    TextView teamCreator;
    @Bind(R.id.team_desc)
    TextView teamDesc;
    @Bind(R.id.team_zongzhi)
    TextView teamZongzhi;
    @Bind(R.id.service_content)
    TextView serviceContent;
    @Bind(R.id.team_yaoqing)
    TextView teamYaoqing;

    private TeamRewardModel rewardModel;
    private String teamCode;
    YaoQingMemberPopup memberPopup;
    private int type;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_team_introduction;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, "团队管理");
    }

    @Override
    protected void setUpViews() {
        type = getIntent().getExtras().getInt("type");
        teamCode = getIntent().getExtras().getString(Constants.TEAM_CODE);
        if (type == 1) { //临时团队
            teamYaoqing.setVisibility(View.GONE);
        }
        TeamAPI.getTeamAllInfo(teamCode, this, getLoading(R.string.watting_hint));
    }

    @OnClick(R.id.team_yaoqing)
    public void yaoqing(View view) {
        if (memberPopup == null) {
            memberPopup = new YaoQingMemberPopup(this);
        }
        memberPopup.setCliclSureListener(new MenuPopuWindow.ClickSureListener() {
            @Override
            public void clickSureLinstener(String... param) {
                TeamAPI.findstaff(teamCode, param[0], TeamIntroductionActivity.this, getLoading(R.string.watting_hint));

            }
        });
        memberPopup.showAtLocation(view, Gravity.CENTER, 0, 0);
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        BaseModel base = JSONObject.parseObject(json, BaseModel.class);
        if (!base.isSuccess()) {
            T.showShort(this, base.msg);
            return;
        }
        if (type == TeamAPI.FIND_STAFF) {
            T.showShort(this, base.msg);
            memberPopup.dismiss();

        } else if (type == TeamAPI.TEAM_DETAIL) {
            JSONObject team = JSONObject.parseObject(base.data);
            TeamDetailModel model = JSONObject.parseObject(team.get("team").toString(), TeamDetailModel.class);
            JSONArray teamReards = JSONArray.parseArray(team.get("teamReward").toString());
            teamName.setText(model.getTeamName());
            serviceContent.setText(TextUtils.isEmpty(model.getServiceContent()) ? "暂无" : model.getServiceContent());
            teamZongzhi.setText(TextUtils.isEmpty(model.getTeamPurpose()) ? "暂无" : model.getTeamPurpose());
            teamDesc.setText(TextUtils.isEmpty(model.getTeamProfile()) ? "暂无" : model.getTeamProfile());
            teamCreateTime.setText(model.getCreateTime());
            if (teamReards != null && !teamReards.isEmpty()) {
                rewardModel = JSONObject.parseObject(teamReards.get(0).toString(), TeamRewardModel.class);
                teamCreator.setText(rewardModel.getUsername());
            }
        }

    }
}
